﻿using Lamex.Base;
using Lamex.Interface;
using Lamex.Library;
using Lamex.Library.CommonClass;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Lamex.mNC.UI
{
    public partial class frmNC_Completed_Register : frmBaseChild
    {
        public string issueNo = "";
        public frmNC_Completed_Register()
        {
            cbIssue_No.Text = issueNo;
            InitializeComponent();
            
        }
        public frmNC_Completed_Register(string ISSUE_NO)
        {
            InitializeComponent();
            if (!string.IsNullOrEmpty(ISSUE_NO))
            {
                DataTable dataTable = Common.GetDataTable.GetNC_Convert_IssueRecord(ISSUE_NO);
                if (dataTable.Rows.Count > 0)
                {
                    //显示相关信息到页面
                    cbIssue_No.Text = ISSUE_NO;
                    cbIssue_No.Text = dataTable.Rows[0]["ISSUE_NO"].ToString();
                    cbIssue_From.Text = dataTable.Rows[0]["ISSUE_FROM"].ToString();
                    txtNumber.Text = dataTable.Rows[0]["PART_OR_DWG_NO"].ToString();
                    cbIssue_Type.Text = dataTable.Rows[0]["ISSUE_CATEGORY"].ToString();
                    txtDesc.Text = dataTable.Rows[0]["ISSUE_DESC"].ToString();
                    txtQty.Text = dataTable.Rows[0]["MO_PART_QTY"].ToString();
                    cbDepart.Text = dataTable.Rows[0]["RESPONSIBLE_DEPT"].ToString();
                    cbMan.Text = dataTable.Rows[0]["PERSON_IN_CHARGE"].ToString();

                    //显示图片
                    pictureBox1.Image = null;
                    string strFileName = ISSUE_NO + ".jpg";
                    string filePath = @"\\TX-FS10\tx-fs1\IMA\MES\ShareResource\NC_Issue_Image\" + strFileName;
                    if (File.Exists(filePath))
                    {
                        //根据路径转换为Byte[]数组
                        byte[] imgBytes = GetImageByPath(filePath);
                        MemoryStream ms = new MemoryStream(imgBytes, 0, imgBytes.Length);
                        //设置图片
                        Image returnImage = Image.FromStream(ms);
                        if (returnImage != null)
                            pictureBox1.Image = returnImage;
                    }
                }
                else
                {
                    DevExpress.XtraEditors.XtraMessageBox.Show("没有该问题编号的数据！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
            }
        }
        private void frmNC_Completed_Register_Load(object sender, EventArgs e)
        {
            base.InitButtons();
            InitInfo();
            
        }
        public void InitInfo()
        {
            //方案更改

            string sql = @"select FIELD_VALUE_OPTION from Sys_ControlValueOption where FIELD_NAME='REASON_AND_ACTION'";
            DataTable dt = BaseBuildSql.GetDataTableBySql(sql);
            if (dt.Rows.Count > 0)
            {
                string[] list = dt.Rows[0]["FIELD_VALUE_OPTION"].ToString().Split(',');
                foreach (var item in list)
                {
                    this.cbChange_Case.Properties.Items.Add(item);
                }
            }
        }
        private void frmNC_Completed_Register_Resize(object sender, EventArgs e)
        {
            resetPictureBox();
        }

        public override void DoSave(IButtonInfo sender)
        {
            try
            {
                string ISSUE_NO = cbIssue_No.Text;
                string strChange_Case = cbChange_Case.Text;

                if (string.IsNullOrEmpty(ISSUE_NO))
                {
                    DevExpress.XtraEditors.XtraMessageBox.Show("问题编号不允许为空,请检查！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
                if (string.IsNullOrEmpty(strChange_Case))
                {
                    DevExpress.XtraEditors.XtraMessageBox.Show("方案更改不允许为空,请检查！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
            
                //更新到数据库
                Common.SQLHelper.Update_NC_Convert_IssueRecordByNo(ISSUE_NO, strChange_Case);
                DevExpress.XtraEditors.XtraMessageBox.Show("登记成功！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (Exception ex)
            {
                DevExpress.XtraEditors.XtraMessageBox.Show(ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
        private void resetPictureBox()
        {
            panelControl4.Width = this.Width / 8 * 5;
            panelControl2.Width = this.Width / 8 * 3;
            pictureBox1.Width = panelControl4.Width - 12;
            pictureBox1.Height = panelControl4.Height - 12;
            pictureBox1.Location = new Point(6, 6);

            if (!(pictureBox1.Image == null))
            {
                if (pictureBox1.Image.Width > pictureBox1.Width || pictureBox1.Image.Height > pictureBox1.Height)
                {
                    pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;
                }
                else
                {
                    pictureBox1.SizeMode = PictureBoxSizeMode.CenterImage;
                }
            }
        }
        /// <summary>
        /// 根据图片路径获取字节
        /// </summary>
        /// <param name="filePath"></param>
        /// <returns></returns>
        public byte[] GetImageByPath(string filePath)
        {
            byte[] buffer = null;
            try
            {
                if (!string.IsNullOrEmpty(filePath))
                {
                    FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read);
                    buffer = new byte[fs.Length];
                    fs.Read(buffer, 0, (int)fs.Length);
                    fs.Close();
                    return buffer;
                }
            }
            catch 
            {
            }
            return buffer;
        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {

            try
            {
                string ISSUE_NO = cbIssue_No.Text;
                string strChange_Case = cbChange_Case.Text;

                if (string.IsNullOrEmpty(ISSUE_NO))
                {
                    DevExpress.XtraEditors.XtraMessageBox.Show("问题编号不允许为空,请检查！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
                if (string.IsNullOrEmpty(strChange_Case))
                {
                    DevExpress.XtraEditors.XtraMessageBox.Show("方案更改不允许为空,请检查！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }

                //更新到数据库
                Common.SQLHelper.Update_NC_Convert_IssueRecordByNo(ISSUE_NO, strChange_Case);
                DevExpress.XtraEditors.XtraMessageBox.Show("登记成功！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (Exception ex)
            {
                DevExpress.XtraEditors.XtraMessageBox.Show(ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void labelGoto_Click(object sender, EventArgs e)
        {
            DataTable dt = BaseBuildSql.GetDataTableBySql("select *　from　sys_menu where winformid='menuNC_Issue_List'");
            MdiTools.OpenChildForm_Param(this.MdiParent as IMdiForm, new frmBaseChildWinForm("NC_Convert_IssueRecord", 0, true, false), dt.Rows[0]["id"].ToString());
        }
    }
}
